/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import android.content.Context;
import android.support.v4.app.FragmentTransaction;
import android.view.inputmethod.InputMethodManager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;

public class TabListener implements ActionBar.TabListener {
	
    protected SherlockFragment mFragment;
    protected final BaseActivity mActivity;
    protected final String mTag;
    protected final Class<? extends SherlockFragment> mClass;
    private final int mTabContainer;
//    private Tab mTab;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tabContainer the tab container [reference]
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(BaseActivity activity, int tabContainer, String tag, Class<? extends SherlockFragment> clz) {
        mActivity = activity;
        mTabContainer = tabContainer;
        mTag = tag;
        mClass = clz;
        mFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
    	
//    	mTab = tab;
//    	
//    	// unselect previous activity unselected tab
//    	TabListener pl = mActivity.getPreviousListener();
//    	if(pl!=null) {
//    		pl.onUnselectPrevious(ft);
//    		mActivity.setPreviousListener(null);
//    	}
    	
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            ft.replace(mTabContainer, newFragment(), mTag);
        } else {
            // If it exists, simply attach it in order to show it
        	if(mFragment.isDetached()) 
        		ft.attach(mFragment);
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
        	
	        // Hide keyboard
	        if(mFragment.isAdded()) {
				InputMethodManager inputManager = 
				        (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE); 
				inputManager.hideSoftInputFromWindow(mFragment.getView().getWindowToken(),
				        InputMethodManager.HIDE_NOT_ALWAYS);
	        }

            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);

        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
    
    public void onUnselectPrevious(FragmentTransaction ft) {
    	
    }
    
    protected SherlockFragment newFragment() {
        mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
        return mFragment;
    }
    
    public SherlockFragment getFragment() {
    	return mFragment;
    }
    
//    public Tab getTab() {
//    	return mTab;
//	}
    
}
